#ifndef __SHADOWTESTER__
#define __SHADOWTESTER__

#include "Tracer.h"
#include "Light.h"
#include "ShadowRay.h"
#include "Ray.h"
#include <vector>

using std::vector;

namespace Engine
{
	class ShadowTester
	{
	public:

		ShadowTester()
		{}

		ShadowTester(Tracer* p_currentTracer,vector<Light*>& p_allLights)
		{
			m_tracerPtr = p_currentTracer;
			m_sceneLights = p_allLights;
		}

		bool TestForShadow(ShadeRec& p_currentDetails)
		{
			for (int lightIndex = 0; lightIndex < m_sceneLights.size(); lightIndex ++ )
			{
				Light* currentLight = m_sceneLights[lightIndex];

				Point3D epsillonCollisionPoint = p_currentDetails.GetkEpsillonCollisionPoint();
				Point3D direction = currentLight->GetDirection(p_currentDetails);
				direction.normalize();
				ShadowRay currentShadowRay(epsillonCollisionPoint, direction);
				ShadeRec currentDetails;
				currentLight->TestIntersection(currentShadowRay, currentDetails);

				if (TestShadowRay(currentShadowRay, currentLight, currentDetails.tmin))
				{
					return true;
				}	
			}
			return false;
		}

		bool TestShadowRay(Engine::ShadowRay& p_currentRay,  Light* p_currentLight, float& p_currentTmin)
		{
			ShadeRec collisionDetails;
			bool intersection = m_tracerPtr->TraceRay(p_currentRay, collisionDetails,true);

			if (intersection && p_currentTmin > collisionDetails.tmin)
				return true;
			else
				return false;
		}

	private:
		Tracer* m_tracerPtr;
		vector<Light*> m_sceneLights;

	};
}
#endif // !__SHADOWTESTER__
